﻿using System;
using System.Data;
using System.Globalization;
using System.Web.UI.WebControls;
using Pantry.Business;
using Pantry.Commons;

namespace Pantry.Web.Users.Controls
{
    public partial class ListTeam : System.Web.UI.UserControl
    {
        private DataView dataView; 
        private static bool isSort = true;

        protected void Page_Load(object sender, EventArgs e)
        {
            var departmentId = Request.QueryString["Id"];
            if (!Validates.IsNaturalNumber(departmentId))
            {
                return;
            }
            Paging();
        }

        public void ButtonCreateClicked(object sender,EventArgs eventArgs)
        {
            var departmentId = Request.QueryString["id"];
            if (String.IsNullOrEmpty(departmentId))
            {
                departmentId = "1";
            }
            Response.Redirect("TeamDetail.aspx?DepartmentId="+departmentId);
        }

        public void FilterIsActive(bool includeIsActive)
        {
            dataView.RowFilter = includeIsActive ? null : "IsActive = 'Yes'";
        }

        public void LinkButtonSortingClicked(object sender, EventArgs eventArgs)
        {
            var button = (LinkButton) sender;
            Sort(button.ID);
        }

        public void ButtonFilterAllClicked(object sender,EventArgs eventArgs)
        {
            var strFillter = string.Empty;
            Fillter(strFillter);
        }

        public void ButtonFilter09(object sender, EventArgs eventArgs)
        {
            const string strFillter = @"
                TeamName LIKE '0%'
                OR TeamName LIKE '1%'
                OR TeamName LIKE '2%'
                OR TeamName LIKE '3%'
                OR TeamName LIKE '4%'
                OR TeamName LIKE '5%'
                OR TeamName LIKE '6%'
                OR TeamName LIKE '7%'
                OR TeamName LIKE '8%'
                OR TeamName LIKE '9%'
            ";
            Fillter(strFillter);
        }

        public void ButtonFilterA(object sender, EventArgs eventArgs)
        {
            const string strFillter = @"
                TeamName LIKE 'A%'
                OR TeamName LIKE 'B%'
                OR TeamName LIKE 'C%'
                OR TeamName LIKE 'D%'
                OR TeamName LIKE 'E%'
            ";
            Fillter(strFillter);
        }

        public void ButtonFilterF(object sender, EventArgs eventArgs)
        {
            const string strFillter = @"
                TeamName LIKE 'A%'
                OR TeamName LIKE 'F%'
                OR TeamName LIKE 'G%'
                OR TeamName LIKE 'H%'
                OR TeamName LIKE 'I%'
            ";
            Fillter(strFillter);
        }

        public void ButtonFilterK(object sender, EventArgs eventArgs)
        {
            const string strFillter = @"
                TeamName LIKE 'K%'
                OR TeamName LIKE 'L%'
                OR TeamName LIKE 'M%'
                OR TeamName LIKE 'N%'
            ";
            Fillter(strFillter);
        }

        public void ButtonFilterO(object sender, EventArgs eventArgs)
        {
            const string strFilter = @"
                TeamName LIKE 'O%'
                OR TeamName LIKE 'P%'
                OR TeamName LIKE 'Q%'
                OR TeamName LIKE 'R%'
            ";
            Fillter(strFilter);
        }

        public void ButtonFilterS(object sender, EventArgs eventArgs)
        {
            const string strFillter = @"
                TeamName LIKE 'S%'
                OR TeamName LIKE 'T%'
                OR TeamName LIKE 'U%'
                OR TeamName LIKE 'V%'
            ";
            Fillter(strFillter);
        }

        public void ButtonFilterW(object sender, EventArgs eventArgs)
        {
            const string strFillter = @"
                TeamName LIKE 'W%'
                OR TeamName LIKE 'X%'
                OR TeamName LIKE 'X%'
                OR TeamName LIKE 'Y%'
                OR TeamName LIKE 'Z%'
            ";
            Fillter(strFillter);
        }
        
        private void Paging()
        {
            var teamBus = new TeamBus();
            var departmentId = Convert.ToInt32(Request.QueryString.Get("Id"), CultureInfo.InvariantCulture);
            var listTeams = teamBus.RetrieveData("DepartmentId",departmentId.ToString(CultureInfo.CurrentCulture),SqlDbType.Int);
            if (listTeams ==null)
            {
                return;
            }
            dataView = listTeams.DefaultView;
            FilterIsActive(CheckBoxIncludeActivate.Checked);
            CollectionPager.PageSize = 10;
            CollectionPager.BindToControl = RepeaterTeams;
            CollectionPager.DataSource = dataView;
            RepeaterTeams.DataSource = CollectionPager.DataSourcePaged;
            RepeaterTeams.DataBind();
        }

        private void Fillter(string fillter)
        {
            var strFillter = "IsActive = 'true'";
            if (CheckBoxIncludeActivate.Checked)
            {
                strFillter = "";
            }

            if (!String.IsNullOrEmpty(fillter)&!String.IsNullOrEmpty(strFillter))
            {
                strFillter += " AND ";
                
            }
            strFillter += fillter;
            dataView.RowFilter = strFillter;
        }

        private void Sort(string field)
        {
            try
            {
                if (isSort)
                {
                    dataView.Sort = string.Format(CultureInfo.InvariantCulture,"{0} DESC", field);
                    isSort = false;
                }
                else
                {
                    dataView.Sort = string.Format(CultureInfo.InvariantCulture,"{0} ASC", field);
                    isSort = true;
                }
            }
            catch (Exception exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                throw;
            }
        }
    }
}